package io.github.junxworks.qt.modules.mm.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import io.github.junxworks.ep.core.orm.TBaseMapper;
import io.github.junxworks.qt.modules.mm.dto.MmExpressionCondition;
import io.github.junxworks.qt.modules.mm.entity.MmExpression;
import io.github.junxworks.qt.modules.mm.vo.MmExpressionVo;

@Mapper
public interface MmExpressionMapper extends TBaseMapper<MmExpression> {

	List<MmExpressionVo> queryList(MmExpressionCondition condition);

	@Select("select * from mm_expression where id=#{id}")
	MmExpressionVo queryMmExpressionById(@Param("id") Long id);

	@Select("select e.*,u.name `create_user_name` from mm_expression e inner join ep_s_user u on e.create_user=u.id where e.exp_name=#{expName} and e.status=0")
	MmExpressionVo queryMmExpressionByName(@Param("expName") String expName);

	@Delete("delete from mm_exp_ds_rela where exp_id=#{id}")
	int deleteExpDsRela(@Param("id") Long id);

	@Delete("delete from mm_exp_exp_rela where exp_id_from=#{id}")
	int deleteExpExpRela(@Param("id") Long id);

	@Delete("delete from mm_exp_var_rela where exp_id=#{id}")
	int deleteExpVarRela(@Param("id") Long id);

	@Select("select count(1) from mm_exp_exp_rela where exp_id_from=#{fromId} and exp_id_to=#{toId}")
	int countExpRela(@Param("fromId") Long fid, @Param("toId") Long tid);

	@Select("SELECT group_concat(e.exp_name) `names` FROM mm_exp_exp_rela eer, mm_expression e WHERE eer.exp_id_to = #{id} AND eer.exp_id_from = e.id AND e.status = 0 GROUP BY eer.exp_id_to")
	String queryRelaExpNamesByExpId(Long id);

	@Select("SELECT e.id  FROM mm_exp_exp_rela eer, mm_expression e WHERE eer.exp_id_from = #{id} AND eer.exp_id_to = e.id AND e.status = 0")
	List<Long> queryRelaExpIdsByExpId(Long id);

	List<Long> queryAllDatastreamIdsByExpIds(@Param("expIds") List<Long> expIds);

	List<Long> queryAllVariablesIdsByExpIds(@Param("expIds") List<Long> expIds);

	List<MmExpressionVo> queryAllExpressionsByIds(@Param("expIds") List<Long> expIds);
}